<?php


namespace app\system\controller\vod;

use app\common\model\vod\CateModel;
use app\system\controller\BaseController;

class CateController extends BaseController
{
  /**
   * 无需登录方法
   * @var array
   */
  protected $noLogin = [];

  /**
   * 不检测权限方法
   * @var array
   */
  protected $noCheck = [];

  /**
   * 权限映射
   * @var array
   */
  protected $mapCheck = [
      '*' => 'index'
  ];
  /**
   * 视频模型
   * @var null
   */
  private $model = null;

  /**
   * 初始化
   */
  public function __construct()
  {
    parent::__construct();
    $this->model = new CateModel();
  }

  /**
   * 视频列表
   * @return string
   * @throws \Exception
   */
  public function index()
  {
    $list = $this->model->getList();
    if($this->request->isAjax()){
      return json(array_values($list));
    }
    $this->assign('list', $list);
    return $this->fetch();
  }

  /**
   * 刷新分类
   * @throws \Exception
   */
  public function refresh()
  {
    $this->model->refresh();
    $this->success('刷新成功');
  }

  /**
   * 添加分类
   * @throws \Exception
   */
  public function add()
  {
    $pid  = $this->request->param('pid/d', -1);
    $name = $this->request->param('name/s');
    $this->validate(['pid' => $pid, 'name' => $name], [
        'pid|父分类' => 'require',
        'name|分类名称' => 'require'
    ]);
    $this->model->add($pid, $name);
    $this->success('操作成功');
  }

  /**
   * editable
   * @return string
   */
  public function editable()
  {
    try {
      $data = $this->request->param();
      $this->validate($data, [
          'pk|标识' => 'number',
          'value|值' => 'require',
      ]);
      $this->model->edit($data['pk'], $data['value']);
    } catch (\Throwable $th) {
      return response($th->getMessage(), 500);
    }
    return 'ok';
  }

  /**
   * 删除分类
   * @throws \Exception
   */
  public function del()
  {
    $id = $this->request->param('id/d');
    $this->model->del($id);
    $this->success('操作成功');
  }

}